<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="2.0"
    xmlns:lotb="http://www.lordsoftheblag.net/"
    >

    <xsl:template match="/">
        <html>
            <xsl:call-template name="header"/>
            <body>
                <xsl:call-template name="script"/>
                <table id='result_table'>
                    <xsl:call-template name="table-header"/>
                    <xsl:for-each-group select="testsuite/testcase" group-by="@classname">
                        <xsl:variable name="ppos" select="position()"/>
                        <xsl:element name="tr">
                            <xsl:attribute name="class">
                                <xsl:choose>
                                    <xsl:when test="count (current-group()/error) != 0">errorClass</xsl:when>
                                    <xsl:when test="count (current-group()/failure) != 0">failClass</xsl:when>
                                    <xsl:otherwise>passClass</xsl:otherwise>
                                </xsl:choose>
                            </xsl:attribute>
                            <td>
                                <xsl:value-of select="substring (current-grouping-key(), 10)"/>
                            </td>
                            <td>
                                <xsl:value-of select="format-number (sum (current-group()/@time), '0.000')"/>
                            </td>
                            <td>
                                <xsl:value-of select="count(current-group())"/>
                            </td>
                            <td>
                                <xsl:value-of select="count(current-group()) - count (current-group()/failure) - count (current-group()/error)"/>
                            </td>
                            <td>
                                <xsl:value-of select="count (current-group()/failure)"/>
                            </td>
                            <td><xsl:value-of select="count (current-group()/error)"/></td>
                            <td>
                                <xsl:element name="a">
                                    <xsl:attribute name="href">
                                        <xsl:text>javascript:showClassDetail('c</xsl:text>
                                        <xsl:value-of select="position()"/>
                                        <xsl:text>',</xsl:text>
                                        <xsl:value-of select="count(current-group())"/>
                                        <xsl:text>)</xsl:text>
                                    </xsl:attribute>
                                    <xsl:text>Detail</xsl:text>
                                </xsl:element>
                            </td>
                        </xsl:element>
                        <xsl:for-each select="current-group()">
                            <xsl:choose>
                                <xsl:when test="error">
                                    <xsl:element name="tr">
                                        <xsl:attribute name='id'>
                                            <xsl:text>ft</xsl:text>
                                            <xsl:value-of select="$ppos"/>
                                            <xsl:text>.</xsl:text>
                                            <xsl:value-of select="position()"/>
                                        </xsl:attribute>
                                        <xsl:attribute name="class">none</xsl:attribute>
                                        <td class='errorCase'>
                                            <div class='testCase'>
                                                <xsl:value-of select="@name"/>
                                            </div>
                                        </td>
                                        <td>
                                            <xsl:value-of select="format-number (@time, '0.000')"/>
                                        </td>
                                        <td colspan='5' align='center'>
                                            <xsl:element name="a">
                                                <xsl:attribute name="href">
                                                    <xsl:text>javascript:showOutput('ft</xsl:text>
                                                    <xsl:value-of select="$ppos"/>
                                                    <xsl:text>.</xsl:text>
                                                    <xsl:value-of select="position()"/>
                                                    <xsl:text>', '</xsl:text>
                                                    <xsl:value-of select="@name"/>
                                                    <xsl:text>')</xsl:text>
                                                </xsl:attribute>
                                                <xsl:text>fail</xsl:text>
                                            </xsl:element>   
                                            <xsl:element name="script">
                                                <xsl:attribute name="language">javascript</xsl:attribute>
                                                <xsl:attribute name="type">text/javascript</xsl:attribute>
                                                <xsl:text>output_list['ft</xsl:text>
                                                <xsl:value-of select="$ppos"/>
                                                <xsl:text>.</xsl:text>
                                                <xsl:value-of select="position()"/>
                                                <xsl:text>'] = '</xsl:text>
                                                <xsl:value-of select="lotb:javify(error)"/>
                                                <xsl:text>';</xsl:text>
                                            </xsl:element>
                                        </td>
                                    </xsl:element>
                                </xsl:when>
                                <xsl:when test="failure">
                                    <xsl:element name="tr">
                                        <xsl:attribute name="id">
                                            <xsl:text>ft</xsl:text>
                                            <xsl:value-of select="$ppos"/>
                                            <xsl:text>.</xsl:text>
                                            <xsl:value-of select="position()"/>
                                        </xsl:attribute>
                                        <xsl:attribute name="class">none</xsl:attribute>
                                        <td class='failCase'>
                                            <div class='testCase'>
                                                <xsl:value-of select="@name"/>
                                            </div>
                                        </td>
                                        <td>
                                            <xsl:value-of select="format-number (@time, '0.000')"/>
                                        </td>
                                        <td colspan='5' align='center'>
                                            <xsl:element name="a">
                                                <xsl:attribute name="href">
                                                     <xsl:text>javascript:showOutput('ft</xsl:text>
                                                    <xsl:value-of select="$ppos"/>
                                                    <xsl:text>.</xsl:text>
                                                    <xsl:value-of select="position()"/>
                                                    <xsl:text>', '</xsl:text>
                                                    <xsl:value-of select="@name"/>
                                                    <xsl:text>')</xsl:text>
                                                </xsl:attribute>
                                                <xsl:text>fail</xsl:text>
                                            </xsl:element>   
                                            <xsl:element name="script">
                                                <xsl:attribute name="language">javascript</xsl:attribute>
                                                <xsl:attribute name="type">text/javascript</xsl:attribute>
                                                <xsl:text>output_list['ft</xsl:text>
                                                <xsl:value-of select="$ppos"/>
                                                <xsl:text>.</xsl:text>
                                                <xsl:value-of select="position()"/>
                                                <xsl:text>'] = '</xsl:text>
                                                <xsl:value-of select='lotb:javify(failure)'/>
                                                <xsl:text>';</xsl:text>
                                            </xsl:element>
                                        </td>
                                    </xsl:element>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:element name="tr">
                                        <xsl:attribute name="id">
                                            <xsl:text>pt</xsl:text>
                                            <xsl:value-of select="$ppos"/>
                                            <xsl:text>.</xsl:text>
                                            <xsl:value-of select="position()"/>
                                        </xsl:attribute>
                                        <xsl:attribute name="class">hiddenRow</xsl:attribute>
                                        <td class='none'>
                                            <div class='testcase'>
                                                <xsl:value-of select="@name"/>
                                            </div>
                                        </td>
                                        <td>
                                            <xsl:value-of select="format-number (@time, '0.000')"/>
                                        </td>
                                        <td colspan='5' align='center'>pass</td>
                                    </xsl:element>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:for-each>                    
                    </xsl:for-each-group>
                    <xsl:call-template name="table-footer"/>
                </table>
                <div id='ending'>
                    <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
                </div>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template name="header">
        <head>
            <title>Test Results</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <xsl:call-template name="style"/>
        </head>
        <xsl:element name="div">
            <xsl:attribute name="class">heading</xsl:attribute>
            <h1>
                <xsl:value-of select="substring(testcase/@classname,10)"/>
            </h1>
            <!--
            <p class='attribute'>
                <strong>Start Time:</strong>
                <xsl:text>2010-05-28 16:05:43</xsl:text>
            </p>
            -->
            <p class='attribute'>
                <strong>Duration: </strong>
                <xsl:value-of select="format-number (testsuite/@time, '0.000')"/>
            </p>
            <p class='attribute'>
                <strong>Status:</strong>
                <xsl:text> Pass </xsl:text>
                <xsl:value-of select="testsuite/@tests - testsuite/@failures - testsuite/@errors"/>
                <xsl:text> Failure </xsl:text>
                <xsl:value-of select="testsuite/@failures + testsuite/@errors"/>
            </p>
            <p class='description'>Tests transformations for Front to Calypso</p>
        </xsl:element>
        <xsl:element name="p">
            <xsl:attribute name="id">show_detail_line</xsl:attribute>
            <p id='show_detail_line'>
                <xsl:text>Show </xsl:text>
                <a href='javascript:showCase(0)'>Summary</a>
                <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
                <a href='javascript:showCase(1)'>Failed</a>
                <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
                <a href='javascript:showCase(2)'>All</a>
            </p>
        </xsl:element>                
    </xsl:template>    

    <xsl:template name="table-header">
        <colgroup>
            <col align='left'/>
            <col align='right'/>
            <col align='right'/>
            <col align='right'/>
            <col align='right'/>
            <col align='right'/>
            <col align='right'/>
        </colgroup>
        <tr id='header_row'>
            <td>Test Group/Test case</td>
            <td>Time</td>
            <td>Count</td>
            <td>Pass</td>
            <td>Fail</td>
            <td>Error</td>
            <td>View</td>
        </tr>
    </xsl:template>
    
    <xsl:template name="table-footer">
        <tr id='total_row'>
            <td>Total</td>
            <td>
                <xsl:value-of select="format-number (testsuite/@time, '0.000')"/>
            </td>
            <td>
                <xsl:value-of select="testsuite/@tests"/>
            </td>
            <td>
                <xsl:value-of select="testsuite/@tests - testsuite/@failures - testsuite/@errors"/>
            </td>
            <td>
                <xsl:value-of select="testsuite/@failures"/>
            </td>
            <td>
                <xsl:value-of select="testsuite/@errors"/>
            </td>
            <td><xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
            </td>
        </tr>
    </xsl:template>
    
    <xsl:template name="style">
        <style type="text/css" media="screen">
            <xsl:text>
body        { font-family: verdana, arial, helvetica, sans-serif; font-size: 80%; }
table       { font-size: 100%; }
pre         { }

/* -- heading ---------------------------------------------------------------------- */
h1 {
}
.heading {
    margin-top: 0ex;
    margin-bottom: 1ex;
}

.heading .attribute {
    margin-top: 1ex;
    margin-bottom: 0;
}

.heading .description {
    margin-top: 4ex;
    margin-bottom: 6ex;
}

/* -- report ------------------------------------------------------------------------ */
#show_detail_line {
    margin-top: 3ex;
    margin-bottom: 1ex;
}
#result_table {
    width: 80%;
    border-collapse: collapse;
    border: medium solid #777;
}
#header_row {
    font-weight: bold;
    color: white;
    background-color: #777;
}
#result_table td {
    border: thin solid #777;
    padding: 2px;
}
#total_row  { font-weight: bold; }
.passClass  { background-color: #6c6; }
.failClass  { background-color: #c60; }
.errorClass { background-color: #c00; }
.passCase   { color: #6c6; }
.failCase   { color: #c60; font-weight: bold; }
.errorCase  { color: #c00; font-weight: bold; }
.hiddenRow  { display: none; }
.testcase   { margin-left: 2em; }


/* -- ending ---------------------------------------------------------------------- */
#ending {
}

            </xsl:text>
        </style>
    </xsl:template>


    <xsl:template name="script">
    <script language="javascript">
        <![CDATA[
<!--
output_list = Array();

/* level - 0:Summary; 1:Failed; 2:All */
function showCase(level) {
    trs = document.getElementsByTagName("tr");
    for (var i = 0; i < trs.length; i++) {
        tr = trs[i];
        id = tr.id;
        if (id.substr(0,2) == 'ft') {
            if (level < 1) {
                tr.className = 'hiddenRow';
            }
            else {
                tr.className = '';
            }
        }
        if (id.substr(0,2) == 'pt') {
            if (level > 1) {
                tr.className = '';
            }
            else {
                tr.className = 'hiddenRow';
            }
        }
    }
}

function showClassDetail(cid, count) {
    var id_list = Array(count);
    var toHide = 1;
    for (var i = 0; i < count; i++) {
        tid0 = 't' + cid.substr(1) + '.' + (i+1);
        tid = 'f' + tid0;
        tr = document.getElementById(tid);
        if (!tr) {
            tid = 'p' + tid0;
            tr = document.getElementById(tid);
        }
        id_list[i] = tid;
        if (tr.className) {
            toHide = 0;
        }
    }
    for (var i = 0; i < count; i++) {
        tid = id_list[i];
        if (toHide) {
            document.getElementById(tid).className = 'hiddenRow';
        }
        else {
            document.getElementById(tid).className = '';
        }
    }
}

function html_escape(s) {
    s = s.replace(/&/g,'&amp;');
    s = s.replace(/</g,'&lt;');
    s = s.replace(/>/g,'&gt;');
    return s;
}

function showOutput(id, name) {
    var w = window.open("", //url
                    name,
                    "resizable,scrollbars,status,width=800,height=450");
    d = w.document;
    d.write("<pre>");
    d.write(html_escape(output_list[id]));
    d.write("\n");
    d.write("<a href='javascript:window.close()'>close</a>\n");
    d.write("</pre>\n");
    d.close();
}
-->
    ]]>
        </script>
    </xsl:template>
    
    <xsl:function name="lotb:javify">
        <xsl:param name="input"/>
        <xsl:variable name='backslash' select='replace ($input, "\\", "\\\\")'/>
        <xsl:variable name='linefeed' select='replace ($backslash, "&#xa;", "\\&#110;")'/>
        <xsl:variable name='result' select='replace ($linefeed, "&#39;", "&#92;&#92;&#39;")'/>
        <xsl:value-of select="$result"/>
    </xsl:function>
    
</xsl:stylesheet>
